package com.shfl.servermanage.logic.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.github.pagehelper.PageInfo;
import com.shfl.servermanage.exception.ApiException;
import com.shfl.servermanage.logic.bean.SysServerSite;
import com.shfl.servermanage.logic.mapper.SysServerSiteMapper;
import com.shfl.servermanage.logic.model.request.AddSiteRequest;
import com.shfl.servermanage.logic.model.request.RemoveByIdRequest;
import com.shfl.servermanage.logic.model.response.ServerSitePageResponse;
import com.shfl.servermanage.logic.model.response.SiteDetailResponse;
import com.shfl.servermanage.logic.service.SysServerSiteService;
import com.shfl.servermanage.pagehelper.PageHelperUtils;
import com.shfl.servermanage.pagehelper.Pageable;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class SysServerSiteServiceImpl implements SysServerSiteService {
    @Autowired
    private SysServerSiteMapper sysServerSiteMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertSelective(SysServerSite record) {
        return sysServerSiteMapper.insertSelective(record);
    }

    @Override
    public PageInfo<ServerSitePageResponse> findPage(Pageable pageable, Map<String, Object> objectMap) {
        PageHelperUtils.startPageWithCount(pageable);
        return new PageInfo<>(sysServerSiteMapper.findPage(objectMap));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(AddSiteRequest addSiteRequest) {
        SysServerSite sysServerSite = new SysServerSite();
        BeanUtil.copyProperties(addSiteRequest, sysServerSite, CopyOptions.create().setIgnoreNullValue(true).setIgnoreError(true));
        sysServerSiteMapper.insertSelective(sysServerSite);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void remove(RemoveByIdRequest removeByIdRequest) {

    }

    @Override
    public SysServerSite selectByPrimaryKey(Long id) {
        return sysServerSiteMapper.selectByPrimaryKey(id);
    }

    @Override
    public void detail(SiteDetailResponse siteDetailResponse, Long id) {
        SysServerSite sysServerSite = sysServerSiteMapper.selectByPrimaryKey(id);
        if (sysServerSite == null) {
            throw new ApiException("1400");
        }
        BeanUtil.copyProperties(sysServerSite, siteDetailResponse);
    }

    @Override
    public List<ServerSitePageResponse> findList(Map<String, Object> objectMap) {
        return sysServerSiteMapper.findList(objectMap);
    }

}
